<?php

namespace app\sales\controller;

use app\admin\common\Base;
use think\Db;
//销售汇总表（按销售人员）
class Salessummarysales extends Base
{
   public function index()
   {
      $data = $this->request->param();
      $month = $this->get_month_start_to_end();
      $this->assign('month', $month);
      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);

         if (isset($data['startdate']) && isset($data['enddate'])) {
            $month = $this->get_month_start_to_end($data['startdate'], $data['enddate']);
         } else {
            $month = $this->get_month_start_to_end();
         }
         $sql = Db::name('order_good')->alias('a')->join('order b', 'a.order_id = b.id')->field('a.*,b.*,a.mark as a_mark,b.mark as b_mark,a.id as a_id,b.id as b_id')->where('o_type', 'in', [1, 2])->where('b.addtime', '>=', $month['month_start'])->where('b.addtime', '<=', $month['month_end'])->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId]);
         $sql = !isset($data['user_id']) || $data['user_id'] == '' ? $sql : $sql->where('b.user_id', $data['user_id']); //客户
         $sql = !isset($data['goods_id']) || $data['goods_id'] == '' ? $sql : $sql->where('a.goods_id', $data['goods_id']); //商品
         $sql = !isset($data['store_id']) || $data['store_id'] == '' ? $sql : $sql->where('a.ck', 'in', $data['store_id']); //仓库
         if (!isset($data['customer_category_id']) || $data['goods_category_id'] == 0 || $data['customer_category_id'] == '') {
         } else {
            $c = Db::name('customer')->where('category_id', $data['customer_category_id'])->select();
            $c['ids'] = implode(',', array_column($c, 'id'));
            $sql->where('b.user_id',  $c['ids']); //客户类别
         }
         $list0 = $sql->order('b.addtime desc,a.id asc')->select();
         $list1 = array();
         $num = []; //数量
         $num1 = []; //数量-为计算单价

         $price = []; //单价
         $hs_price = []; //含税单价
         $jine = []; //销售金额
         $tax_e = []; //税额
         $total_price = []; //价税合计
         foreach ($list0 as $key => $value) {
            $kk = $value['ck'] . $value['goods_id'] . $value['saler']; //分组标识
            $goods = Db::name('goods')->where('id', $value['goods_id'])->find();
            $auth = Db::name('auth_user')->where('id', $value['saler'])->find();
            if ($value['o_type'] == 2) {
               if (!isset($num[$kk])) {
                  $num[$kk] = $value['num'] * -1;
                  $jine[$kk] = $value['jine'] * -1;
                  $tax_e[$kk] = $value['tax_e'] * -1;
                  $total_price[$kk] = $value['total_price'] * -1;
               } else {
                  $num[$kk] = $num[$kk] - $value['num'];
                  $jine[$kk] = $jine[$kk] - $value['jine'];
                  $tax_e[$kk] = $tax_e[$kk] - $value['tax_e'];
                  $total_price[$kk] = $total_price[$kk] - $value['total_price'];
               }
            } else {
               if (!isset($num[$kk])) {
                  $num[$kk] = $value['num'];
                  $jine[$kk] = $value['jine'];
                  $tax_e[$kk] = $value['tax_e'];
                  $total_price[$kk] = $value['total_price'];
               } else {
                  $num[$kk] = $num[$kk] + $value['num'];
                  $jine[$kk] = $jine[$kk] + $value['jine'];
                  $tax_e[$kk] = $tax_e[$kk] + $value['tax_e'];
                  $total_price[$kk] = $total_price[$kk] + $value['total_price'];
               }
               //$num=$num+$value['num'];
            }
            if (!isset($num[$kk])) {
               $num1[$kk] = $value['num'];
               $price[$kk] = $value['price'];
               $hs_price[$kk] = $value['hs_price'];
            } else {
               $num1[$kk] = $num[$kk] + $value['num'];
               $price[$kk] = $num[$kk] + $value['price'];
               $hs_price[$kk] = $num[$kk] + $value['hs_price'];
            }

            foreach ($value as $ke => $va) {
               $list1[$kk][$ke] = $va;
               //$list1[$kk]['type_name']=$value['o_type'] == 1 ? '销货' : '退货';
               $list1[$kk]['goods_cate'] =  Db::name('goods_category')->where('id', $goods['goods_category_id'])->value('name');
               $list1[$kk]['goods_sn'] = $goods['sn'];
               $list1[$kk]['goods_name'] = $goods['name'];
               $list1[$kk]['goods_code'] = $goods['code'];
               $list1[$kk]['goods_model'] = $goods['model'];
               $list1[$kk]['unit_name'] = Db::name('unit')->where('id', $value['dw'])->value('name');
               $list1[$kk]['store_name'] = Db::name('store')->where('id', $value['ck'])->value('name');
               $list1[$kk]['auth_name'] = $auth['name'];
            }
         }
         //halt($num);
         foreach ($list1 as $k => &$v) {
            unset($v['id']);
            $v['number'] = $num[$k];
            $v['ave_jine'] = $jine[$k];
            $v['ave_tax_e'] = $tax_e[$k];
            $v['ave_total_price'] = $total_price[$k];
            $v['ave_price'] = $num1[$k] == 0 ? '0' : $price[$k] / $num1[$k];
            $v['ave_hs_price'] = $num1[$k] == 0 ? '0' : $hs_price[$k] / $num1[$k];
            $v['danweichengben'] = 0; //单位成本
            $v['xiaoshouchengben'] = 0; //销售成本
            $v['xiaoshoumaoli'] = 0; //销售毛利
            $v['maolilv'] = 0; //毛利率

         }
         //halt($list1);
         $count = count($list1);
         $list = array_slice($list1, $start, $limit);

         //halt($list);
         return $this->grid_data($list, $count, $page, $limit);
      }

      $customer = Db::name('customer')->where('is_customer', 'in', '1,3')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
      $goods = Db::name('goods')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();

      $this->assign('customer', $customer);
      $this->assign('goods', $goods);
      return $this->fetch();
   }

   public function add()
   {
      $data = $this->request->param();


      return $this->fetch();
   }
}
